//------------------------------------------------------------------------------
// <copyright company="Telligent Systems">
//     Copyright (c) Telligent Systems Corporation.  All rights reserved.
// </copyright> 
//------------------------------------------------------------------------------

using System;
using System.Collections;
using System.Web.UI.WebControls;
using CommunityServer.Components;
using CommunityServer.ControlPanel.UI;
using CommunityServer.Controls;
using CommunityServer.Spam;
using ResourceManager = CommunityServer.ControlPanel.Components.ResourceManager;

namespace CommunityServerWeb.ControlPanel.Tools
{
	/// <summary>
	/// Summary description for ManageSpamRules.
	/// </summary>
	public class ManageSpamRules : BaseToolsPage
	{
		#region Members
		protected StatusMessage Status;
		protected Panel ConfigurationPanel;
		protected TextBox AutoModerateValue;
		protected TextBox AutoDeleteValue;
		protected CheckBox EnableAudit;
		protected RequiredFieldValidator AutoModerateValueValidator;
		protected RequiredFieldValidator AutoDeleteValueValidator;
		protected Literal AutoModerateLabel;
		protected Literal AutoDeleteLabel;
		protected Literal EnableAuditLabel;
		protected Repeater RulesList;
		protected IButton SaveButton;
		protected IButton RefreshButton;

		private ArrayList ConfiguredRules;
		#endregion

		protected override void OnInit(EventArgs e)
		{
			this.SaveButton.Click += new EventHandler(this.SaveButton_Click);
			this.RefreshButton.Click += new EventHandler(this.RefreshButton_Click);
			this.RulesList.ItemDataBound += new RepeaterItemEventHandler(this.RulesList_ItemDataBound);
			this.Load += new EventHandler(this.Page_Load);
			base.OnInit(e);
		}
		
		private void Page_Load(object sender, EventArgs e)
		{
			if (!Page.IsPostBack)
				this.DataBind();
		}

		public override void DataBind()
		{
			Status.Visible = false;
			RefreshButton.Text = ResourceManager.GetString("Spam_ManageSpamRules_Refresh");

			AutoModerateLabel.Text = ResourceManager.GetString("Spam_ManageSpamRules_AutoModerate");
			AutoDeleteLabel.Text = ResourceManager.GetString("Spam_ManageSpamRules_AutoDelete");
			EnableAuditLabel.Text = ResourceManager.GetString("Spam_ManageSpamRules_EnableAudit");

			SpamConfiguration config = SpamConfiguration.Instance();
			AutoModerateValue.Text = config.AutoModerateScore.ToString();
			AutoDeleteValue.Text = config.AutoDeleteScore.ToString();
			EnableAudit.Checked = config.EnableSpamScoreAudit;

			ConfiguredRules = SpamRules.GetSelectedRules();
			RulesList.DataSource = SpamRules.SearchForRules();
			RulesList.DataBind();
		}

		private void SaveSettings()
		{
			SpamConfiguration config = SpamConfiguration.Instance(false);

			config.AutoModerateScore = Int32.Parse(AutoModerateValue.Text.Trim());
			config.AutoDeleteScore = Int32.Parse(AutoDeleteValue.Text.Trim());
			config.EnableSpamScoreAudit = EnableAudit.Checked;

			// Save the rules that are checked
			ArrayList allRules = SpamRules.SearchForRules();
			ArrayList checkedRules = new ArrayList();
			for (int i = 0; i < RulesList.Items.Count; i++)
			{
				CheckBox cb = RulesList.Items[i].FindControl("RuleEnabled") as CheckBox;
				if ((cb != null) && (cb.Checked))
				{
					Literal l = RulesList.Items[i].FindControl("RuleID") as Literal;
					if (l != null)
					{
						Guid ruleID = new Guid(l.Text);
						SpamRule rule = FindRule(allRules, ruleID);
						if (rule != null)
							checkedRules.Add(rule);
					}
				}
			}

			SpamRules.SaveRules(checkedRules, config);
		}

		private SpamRule FindRule(ArrayList rules, Guid ruleID)
		{
			if (rules != null)
			{
				foreach (SpamRule rule in rules)
				{
					if (rule.RuleID == ruleID)
						return rule;
				}
			}
			return null;
		}

		private bool IsRuleEnabled(Guid ruleID)
		{
			if (ConfiguredRules != null)
			{
				foreach (SpamRule rule in ConfiguredRules)
				{
					if (rule.RuleID == ruleID)
						return true;
				}
			}
			return false;
		}

		#region Events

		private void SaveButton_Click(Object sender, EventArgs e)
		{
			if (!Page.IsValid)
				return;

			SaveSettings();

			Status.Visible = true;
			Status.Success = true;
			Status.Text = ResourceManager.GetString("Spam_ManageSpamRules_SaveSuccessful");
		}

		private void RefreshButton_Click(Object sender, EventArgs e)
		{
			SpamRules.ClearRulesCache();
			ConfiguredRules = SpamRules.GetSelectedRules();
			RulesList.DataSource = SpamRules.SearchForRules();
			RulesList.DataBind();

			Status.Visible = true;
			Status.Success = true;
			Status.Text = ResourceManager.GetString("Spam_ManageSpamRules_RuleReloaded");
		}

		private void RulesList_ItemDataBound(object sender, RepeaterItemEventArgs e)
		{
			if ((e.Item.ItemType == ListItemType.Item) || (e.Item.ItemType == ListItemType.AlternatingItem))
			{
				SpamRule rule = e.Item.DataItem as SpamRule;
				if (rule != null)
				{
					Literal RuleID = e.Item.FindControl("RuleID") as Literal;
					if (RuleID != null)
						RuleID.Text = rule.RuleID.ToString();

					Literal RuleName = e.Item.FindControl("RuleName") as Literal;
					if (RuleName != null)
						RuleName.Text = rule.Name;

					Literal RuleDesc = e.Item.FindControl("RuleDesc") as Literal;
					if (RuleDesc != null)
						RuleDesc.Text = rule.Description;

					CheckBox RuleEnabled = e.Item.FindControl("RuleEnabled") as CheckBox;
					if (RuleEnabled != null)
						RuleEnabled.Checked = IsRuleEnabled(rule.RuleID);

					HyperLink Settings = e.Item.FindControl("Settings") as HyperLink;
					if (Settings != null)
					{
						// Only show configure button if there are actually settings to configure
						ArrayList availableSettings = rule.GetAvailableSettings();
						if ((availableSettings != null) && (availableSettings.Count > 0))
						{
							int height = 75 + (85 * availableSettings.Count);
							Settings.Text = ResourceManager.GetString("Spam_ManageSpamRules_Configure");
							Settings.NavigateUrl = String.Format("javascript:Telligent_Modal.Open('SpamRuleSettings.aspx?ruleid={0}', 500, {1}, null);", rule.RuleID, height);
						}
						else
						{
							Settings.Visible = false;
						}
					}
				}
			}
		}

		#endregion
	}
}
